//
//  MGTwitterEngine+CocoaFan.m
//  CocoaFan
//
//  Created by Chen Xianan on 2/16/09.
//  Copyright 2009 realazy.org All rights reserved.
//

#import "MGTwitterEngine+CocoaFan.h"

#define MAX_MESSAGE_LENGTH      140
#define HTTP_POST_METHOD        @"POST"

@interface MGTwitterEngine (PrivateMethods)

- (NSString *)_queryStringWithBase:(NSString *)base parameters:(NSDictionary *)params prefixed:(BOOL)prefixed;
// Connection/Request methods
- (NSString *)_sendRequestWithMethod:(NSString *)method path:(NSString *)path queryParameters:(NSDictionary *)params body:(NSString *)body requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType;
@end

@implementation MGTwitterEngine (CocoaFan)

- (NSString *)sendFanfouUpdate:(NSString *)status
{
    return [self sendFanfouUpdate:status inReplyTo:@""];
}


- (NSString *)sendFanfouUpdate:(NSString *)status inReplyTo:(NSString *)updateID
{
    if (!status) {
        return nil;
    }
    
    NSString *path = @"statuses/update.xml";
    
    NSString *trimmedText = status;
    if ([trimmedText length] > MAX_MESSAGE_LENGTH) {
        trimmedText = [trimmedText substringToIndex:MAX_MESSAGE_LENGTH];
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:trimmedText forKey:@"status"];
    if (![updateID isEqualToString:@""]) {
        [params setObject:updateID forKey:@"in_reply_to_status_id"];
    }
    NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];
    
    return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path 
                        queryParameters:params body:body 
                            requestType:MGTwitterStatusSend 
                           responseType:MGTwitterStatus];
}

- (NSString *)getFanfouFollowedTimelineSinceID:(NSString *)updateID
										 count:(int)count
{
	NSString *path = @"statuses/friends_timeline.xml";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    if (![updateID isEqualToString:@""]) {
        [params setObject:updateID forKey:@"since_id"];
    }
	if (count > 0){
		[params setObject:[NSString stringWithFormat:@"%d", count] forKey:@"count"];
	}
	[params setObject:@"html" forKey:@"format"];
    
    return [self _sendRequestWithMethod:nil path:path queryParameters:params body:nil 
                            requestType:MGTwitterStatusesRequest 
                           responseType:MGTwitterStatuses];
}

- (NSString *)getFanfouRepliesSinceID:(NSString *)updateID
								count:(int)count
{
	NSString *path = @"statuses/replies.xml";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    if (![updateID  isEqualToString:@""]){
		[params setObject:updateID forKey:@"since_id"];
    }
	if (count > 0){
		[params setObject:[NSString stringWithFormat:@"%d", count] forKey:@"count"];
	}
    [params setObject:@"html" forKey:@"format"];
    return [self _sendRequestWithMethod:nil path:path queryParameters:params body:nil 
                            requestType:MGTwitterRepliesRequest 
                           responseType:MGTwitterStatuses];
}

- (NSString *)getFanfouDirectMessagesSinceID:(int)updateID
									   count:(int)count
{
	NSString *path = @"direct_messages.xml";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    if (updateID != 0) {
        [params setObject:[NSNumber numberWithInt:updateID] forKey:@"since_id"];
    }
	if (count > 0){
		[params setObject:[NSString stringWithFormat:@"%d", count] forKey:@"count"];
	}
    return [self _sendRequestWithMethod:nil path:path queryParameters:params body:nil 
                            requestType:MGTwitterDirectMessagesRequest 
                           responseType:MGTwitterDirectMessages];
}

- (NSString *)deleteFanfouUpdate:(NSString *)updateID
{
    NSString *path = [NSString stringWithFormat:@"statuses/destroy/%@.xml", updateID];
    
    return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path queryParameters:nil body:nil 
                            requestType:MGTwitterAccountRequest 
                           responseType:MGTwitterGeneric];
}

- (NSString *)sendFanfouPrivateMessage:(NSString *)message
									to:(NSString *)userid
							 inReplyTo:(int)msgid;

{
    if (!message || !userid) {
        return nil;
    }
    
    NSString *path = @"direct_messages/new.xml";
    
    NSString *trimmedText = message;
    if ([trimmedText length] > MAX_MESSAGE_LENGTH) {
        trimmedText = [trimmedText substringToIndex:MAX_MESSAGE_LENGTH];
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:trimmedText forKey:@"text"];
    [params setObject:userid forKey:@"user"];
	NSLog(@"msgid: %i", msgid);
	if (msgid > 0) [params setObject:[NSString stringWithFormat:@"%d", msgid] forKey:@"in_reply_to_id"];
    NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];
    return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path 
                        queryParameters:params body:body 
                            requestType:MGTwitterDirectMessageSend
                           responseType:MGTwitterGeneric];
}

@end
